#include "conversion.h"
#include "stl-util.h"
#include <stdlib.h>

namespace tinyscript{
namespace internal{

namespace {

template< typename T > 
double InternalString2Double( const std::vector<T>& str , size_t start , size_t end );

template<>
double InternalString2Double<char>( const std::vector<char>& str , size_t start , size_t end ) {
	// Ugly crack of the original buffer, just to avoid copy 
	std::vector<char>& mutabl_ref = const_cast< std::vector<char_t>& >(str);
	const char c = mutabl_ref[end];
	mutabl_ref[end] = 0;
	double ret = static_cast<double>( atof(tinyscript::util::VectorAsRawArray(str,start) ) );
	mutabl_ref[end] = c;
	return ret;
}

template< typename T >
int InternalString2Int( const std::vector<T>& str , size_t start , size_t end );

template<>
int InternalString2Int<char>( const std::vector<char>& str, size_t start , size_t end ) {
	// Ugly crack of the original buffer, just to avoid copy 
	std::vector<char_t>& mutabl_ref = const_cast< std::vector<char_t>& >(str);
	const char c = mutabl_ref[end];
	mutabl_ref[end] = 0;
	int ret = atoi( tinyscript::util::VectorAsRawArray(str,start) );
	mutabl_ref[end] = c;
	return ret;
}

}

double StringToDouble( const std::vector<char_t>& str , size_t start , size_t end ) {
	return InternalString2Double<char_t>(str,start,end);
}

int StringToInt( const std::vector<char_t>& str , size_t start , size_t end ) {
	return InternalString2Int<char_t>(str,start,end);
}

}
}